package com.yunxiangyue.login

import com.yunxiangyue.logger.Logger
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlin.random.Random

object PhoneMessageLogin {
    private const val TAG = "PhoneMessageLogin"
    private val scope = CoroutineScope(Dispatchers.IO)

    fun init() {
        TencentMessageApi.init()
    }

    fun sendVerifyMessage(phoneNumber: String): String {
        return if (checkPhoneNumber(phoneNumber)) {
            val verifyCode = Random.nextInt(1000000).toString()
            scope.launch {
                TencentMessageApi.sendMessgae(phoneNumber, verifyCode, "5")
            }
            Logger.debug(TAG, "verifyCode: $verifyCode")
            verifyCode
        } else {
            Logger.error(TAG, "Phone Number is wrong!")
            "-1"
        }
    }

    private fun checkPhoneNumber(phoneNumber: String): Boolean {
        return phoneNumber[0] == '+' && phoneNumber.length == 14
    }
}